package com.zllkj.mm.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.appcompat.widget.LinearLayoutCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zllkj.mm.R
import com.zllkj.mm.common.ClientConfig
import com.zllkj.mm.module.goods.vm.ItemCollectionsVM
import com.zllkj.mm.module.goods.vm.ItemCommentVM
import com.zllkj.mm.module.shopping.vm.ItemAfterSaleListVM
import com.zllkj.mm.util.common.PictureLoadUtil
import java.math.BigDecimal

class CommentListAdapter(layoutResId: Int, data: List<ItemCommentVM?>?) :
        BaseQuickAdapter<ItemCommentVM, BaseViewHolder>(layoutResId, data) {
    var sonCommentClickedListener : SonCommentClickedListener? =null
    override fun convert(baseViewHolder: BaseViewHolder, vm: ItemCommentVM) {
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_comment_username).text = vm.name
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_comment_content).text = vm.content
        baseViewHolder.getView<AppCompatTextView>(R.id.tv_comment_time).text = vm.time
        PictureLoadUtil.loadPic(baseViewHolder.getView<AppCompatImageView>(R.id.iv_comment_user_icon),vm.url)
        baseViewHolder.addOnClickListener(R.id.tv_replay_text)
        baseViewHolder.addOnClickListener(R.id.view_sub_comment_area)
        if (vm.subList.size > 0) {
            var area = baseViewHolder.getView<LinearLayoutCompat>(R.id.view_sub_comment_area)
            area.visibility = View.VISIBLE
            area.removeAllViews()


            vm.subList.mapIndexed { index, it ->
                var subView = LayoutInflater.from(mContext).inflate(R.layout.item_comment_list,null)
                subView.findViewById<AppCompatTextView>(R.id.tv_comment_username).text = it.nikeName + " ▶ " + it.beReplyNikeName
                subView.findViewById<AppCompatTextView>(R.id.tv_comment_content).text = it.content
                subView.findViewById<AppCompatTextView>(R.id.tv_comment_time).text = it.createTime
                PictureLoadUtil.loadPic(subView.findViewById<AppCompatImageView>(R.id.iv_comment_user_icon),it.userLogo)
                baseViewHolder.getView<LinearLayoutCompat>(R.id.view_sub_comment_area).addView(subView)
                subView.findViewById<AppCompatTextView>(R.id.tv_replay_text).setOnClickListener {
                    sonCommentClickedListener?.let {
                        it.onClicked(data.indexOf(vm),index)
                    }
                }
            }
        }


    }

    interface SonCommentClickedListener{
       fun onClicked(parentPosition:Int,subPosition:Int)
    }
}